

from textual.message import Message
from textual.reactive import var
from textual.widgets import Input

from httpx import URL

class Omnibox(Input):

    visiting: var[str] = var("")

    def watch_visiting(self) -> None:
        self.placeholder = self.visiting or "输入内容以便搜索"
        if self.visiting:
            self.value = self.visiting

    class RemoteViewCommand(Message):
        """The remote file view command."""

        def __init__(self, url: URL) -> None:
            """Initialise the remove view command.

            Args:
                url: The URL of the remote file to view.
            """
            super().__init__()
            self.url = url
            """The URL of the file to view."""

    class OnSubmitCommand(Message):
        '''提交事件'''
        def __init__(self, text: str) -> None:
            super().__init__()
            self.text = text

    class OnSpkUriCommand(Message):
        '''spk 消息指令'''
        def __init__(self, uri :str):
            self.uri = uri

    def on_input_submitted(self, event: Input.Submitted) -> None:
        '''输入框提交内容时'''
        submitted = self.value.strip()
        '''获取输入框内容'''
        self.value = ""
        '''清空输入框'''

        if submitted.startswith("spk://"):
            self.post_message(self.OnSpkUriCommand(submitted))
            return

        self.post_message(self.OnSubmitCommand(submitted))
        '''发出已经提交消息'''

        event.stop()
        ''''''

if __name__ == "__main__":

    from textual.app import App, ComposeResult

    class OmniboxApp(App):
        def compose(self) -> ComposeResult:
            yield Omnibox(classes="")

        def on_omnibox_on_submit_command(self, event: Omnibox.OnSubmitCommand) -> None:
            self.notify(title='Omnibox Message', message=event.text)

    app = OmniboxApp()
    app.title = 'Omnibox app'.title()
    app.run()